package com.lyz.moil.web;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.webapp.WebAppContext;

public class JettyServer_services {

//    public static void main(String[] args) throws Exception{
//        Server server = new Server();
//        SslSelectChannelConnector ssl_connector = new SslSelectChannelConnector();
//        ssl_connector.setPort(8443);
//        SslContextFactory cf = ssl_connector.getSslContextFactory();
//        cf.setKeyStore("D:/home/tomcat.keystore");
//        cf.setTrustStore("D:/home/tomcat.keystore");
//        cf.setKeyStorePassword("lijiang123123");
//        cf.setTrustStorePassword("lijiang123123");
//        cf.setKeyManagerPassword("lijiang123123");
//        server.addConnector(ssl_connector);
//        WebAppContext context = new WebAppContext();
//        context.setContextPath("/");
//        context.setWar("src/main/webapp");
//        context.setDefaultsDescriptor("./src/test/resources/webdefault.xml");
//        server.setStopAtShutdown(true);
//        server.setHandler(context);
//        server.start();
//        server.join();
//    }

	public static void main(String[] args) throws Exception {
		long start = System.currentTimeMillis();
		Server jettyServer = null;
		try {
			jettyServer = new Server();
			SelectChannelConnector conn = new SelectChannelConnector();
			conn.setUseDirectBuffers(false);// false为使用分配内存
			conn.setHost(args != null && args.length > 0 ? args[0] : "0.0.0.0");
			conn.setPort(args != null && args.length > 1 ? Integer.parseInt(args[1]) : 8686);
			jettyServer.setConnectors(new Connector[] { conn });
			jettyServer.setAttribute("scanIntervalSeconds", 0);
			WebAppContext context = new WebAppContext();
			context.setContextPath("/");
			context.setWar("src/main/webapp");
			context.setDefaultsDescriptor("./src/test/resources/webdefault.xml");
			// scanIntervalSeconds
			jettyServer.setStopAtShutdown(true);
			jettyServer.setHandler(context);
			QueuedThreadPool p = new QueuedThreadPool();
			p.setMinThreads(10);
			p.setMaxThreads(1000);
			jettyServer.setThreadPool(p);
			jettyServer.setHandler(context);
			jettyServer.start();
//			jettyServer.start();
			jettyServer.join();

		} catch (Exception ignore) {
			if (jettyServer != null) {
				try {
					jettyServer.stop();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}

		System.out.println("Jetty started in " + (System.currentTimeMillis() - start) + " ms.");
	}
}
